package com.education.user.controller;


import com.education.common.pojo.SysUserOther;
import com.education.common.pojo.userData;
import com.education.common.utils.*;
import com.education.user.config.BaiduAipContentCensor;
import com.education.user.feign.AuthClient;
import com.education.user.feign.WisdomFeign;
import com.education.user.pojo.*;
import com.education.user.service.ISysUserService;
import com.education.user.service.SysAppUserService;
import com.education.user.service.SysUserRoleServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 系统用户 信息操作处理
 *
 * @author d
 * @date 2020-01-17
 */

@RestController
@RequestMapping("/k12main/sysUser")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private SysAppUserService appUserService;
    @Autowired
    private SysUserRoleServiceImpl userRoleService;
    @Autowired
    private AuthClient authClient;
    @Autowired
    private WisdomFeign wisdomFeign;
    @Autowired
    private BaiduAipContentCensor baiduAipContentCensor;


    @PostMapping("/updateByUserId")
    public int updateByUserId(Integer userId) {
        SysUser user = new SysUser();
        user.setId(userId);
        user.setClassId(0);
        user.setSchoolId(0);
        user.setSysState(1);
        return sysUserService.updateById(user);
    }

    @PostMapping("/updatePriceByUserId")
    public int updatePriceByUserId(@RequestParam Integer id, @RequestParam BigDecimal price) {
        SysUser user = new SysUser();
        user.setId(id);
        user.setPrice(price);
        return sysUserService.updateById(user);
    }


    @GetMapping("/selectBySchooleId")
    public List<Integer> selectBySchooleId(Integer schoolId) {
        List<Integer> list = new ArrayList<>();
        SysUser user = new SysUser();
        user.setSchoolId(schoolId);
        List<SysUser> users = sysUserService.select(user);
        for (SysUser sysUser : users) {
            list.add(sysUser.getId());
        }
        return list;
    }


    /**
     * feign : 根据用户id修改用户达人币
     */
    @PutMapping("/updateByUserId")
    public void updateByUserId(@RequestBody Map<String, Object> map) {
        SysUser user = new SysUser();
        user.setId(Integer.parseInt(map.get("id").toString()));
        //user.setId((Integer) map.get("id"));
        user.setPrice(new BigDecimal(map.get("price").toString()));
        sysUserService.updateById(user);
    }

    /**
     * feign : 根据用户id查询手机号
     */
    @GetMapping("/selectBySchoolIdAndType")
    public Map<String, Object> selectBySchoolIdAndType(Integer schoolId, String type) {
        Map<String, Object> map = new HashMap<>();
        SysUser user = new SysUser();
        user.setSchoolId(schoolId);
        //类型(1学生 2老师 3教务主任)3管理员
        user.setSysType(type);
        List<SysUser> users = sysUserService.select(user);

        if (users.size() != 0) {
            map.put("userId", users.get(0).getId());
        }
        return map;
    }


    /**
     * 达人币明细
     */
    @GetMapping("/search")
    public ApiReturnObject search(SysUser user) {
        List<SysUser> users = sysUserService.findByLikePhone(user);
        Integer integer = sysUserService.selectSumPrice(user);
        Map<String, Object> map = new HashMap<>();
        map.put("sum", integer);
        map.put("list", users);
        return new ApiReturnObject("200", "success", map);
    }

    /**
     * feign : 根据用户id查询手机号
     */
    @GetMapping("/selectByUserId")
    public Map<String, Object> selectByUserId(Integer userId) {
        Map<String, Object> map = new HashMap<>();
        SysUser user = sysUserService.selectByPrimaryKey(userId);
        if (user != null) {
            map.put("phone", user.getSysPhone());
            map.put("name", user.getSysRelname());
            map.put("image", user.getSysIco());
            map.put("CustomerType", user.getCustomerType());
            map.put("SchoolId", user.getSchoolId());
            map.put("price", user.getPrice());
        }
        return map;
    }

    /**
     * 查询系统用户列表
     */
    @PostMapping("/login")
    public ApiReturnObject user(@RequestBody SysUser user) {
        //public ApiReturnObject user( SysUser user) {
        //先判断账号   然后判断密码
        SysUser sysUser = sysUserService.selectSysUserBysysPhone(user.getSysPhone());
        if (sysUser != null) {
            if (sysUser.getSysPassword() != null && sysUser.getSysPassword().length() > 0) {
                if (sysUser.getSysPassword().equals(Token.getMD5String(user.getSysPassword()))) {
                    return new ApiReturnObject("200", "登陆成功", sysUser);
                } else {
                    return new ApiReturnObject("201", "密码错误", "");
                }
            } else {
                return new ApiReturnObject("201", "请输入密码", "");
            }
        } else {
            return new ApiReturnObject("201", "用户名不存在", "");
        }
    }

    /**
     *  提取分页公共代码
     */
    /*private ApiReturnObject paging(T t,String type,Integer pageNum,Integer pageSize){
        if(t.getSchoolId() != null){
            //根据学校id 查出部门列表
            if(pageNum != null && pageSize != null){
                PageHelper.startPage(pageNum,pageSize);
            }
            if("App".equals(type)){
                List<SysAppUser> appUserList = appUserService.select(appUser);
            }
            List<SysUser> userList = sysUserService.listAll(sysUser);

            PageInfo<SysUser> pageInfo = new PageInfo<>(userList);
            return new ApiReturnObject("200","查询成功",pageInfo);
        }
        return new ApiReturnObject("201","缺少schoolId","");
    }*/

    /**
     * 重置密码
     */
    @GetMapping("/resetPassword")
    private ApiReturnObject resetPassword(String id) {
        if (id != null) {
            SysUser user = sysUserService.selectOne(id);
            if (user != null) {
                user.setSysPassword(Token.getMD5String("123456"));
            }
            sysUserService.update(user);
            return new ApiReturnObject("200", "密码已重置为123456", "");
        }
        return new ApiReturnObject("201", "缺少相关参数", "");
    }

    /**
     * 重置密码
     */
    @GetMapping("/resetPasswordtea")
    private ApiReturnObject resetPasswordtea(String phone) {
        if (phone != null) {
            SysUser user = sysUserService.selectTwo(phone);
            if (user != null) {
                user.setSysPassword(Token.getMD5String("123456"));
            }
            sysUserService.update(user);
            return new ApiReturnObject("200", "密码已重置为123456", "");
        }
        return new ApiReturnObject("201", "缺少相关参数", "");
    }

    /**
     * 预约录制时查询讲授人
     */
    @GetMapping("/list")
    public ApiReturnObject listPCUser(SysUser sysUser) {
        //姓名 账号 工号 用户角色 所属部门
        //根据部门进行筛选
        if (sysUser.getSchoolId() != null) {
            //根据学校id 查出部门列表
            List<SysUser> userList = sysUserService.selectUserListOther(sysUser);
            return new ApiReturnObject("200", "查询成功", userList);
        }
        return new ApiReturnObject("201", "缺少schoolId", "");
    }

    /**
     * 预约录制时查询讲授人
     */
    @GetMapping("/listAddRoom")
    public List<Map> listAddRoom(Integer deptId, Integer userId) {
        SysUser user = new SysUser();
        user.setDeptId(deptId);
        user.setId(userId);
        //根据部门id 查出教师列表
        List<SysUser> userList = sysUserService.selectUserListRoom(user);
        ArrayList<Map> maps = new ArrayList<>();
        for (SysUser sysUser : userList) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("label", sysUser.getLabel());
            map.put("value", sysUser.getValue());
            maps.add(map);
        }
        return maps;
    }

    /**
     * 管理端-APP管理-用户管理
     * 查询系统用户列表
     */
    @GetMapping("/listPCUser")
    public ApiReturnObject listPCUser(SysUser sysUser, Integer pageSize, Integer pageNum) {
        //根据学校id 查出部门列表
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<SysUserVO> userList = sysUserService.selectUserList(sysUser);
        for (SysUserVO user : userList) {
            Date date = new Date();
            if (StringUtils.isNotNull(user.getCreateTime())) {
                date = DateUtil.parse(user.getCreateTime());
            }
            String format = DateUtil.format(date);
            user.setCreateTime(format);
            if (user.getSchoolId() != null) {
                String s = wisdomFeign.fingById(user.getSchoolId());
                user.setSchoolName(s);
            }
        }
        PageInfo<SysUserVO> pageInfo = new PageInfo<>(userList);
        return new ApiReturnObject("200", "查询成功", pageInfo);
    }

    /**
     * 查询app用户列表  学生用户表
     */
    @GetMapping("/listAppUser")
    public ApiReturnObject listAppUser(SysAppUser appUser, Integer pageSize, Integer pageNum) {
        //姓名 账号 学校 班级  是否绑定学校
        //根据部门进行筛选
//        if(appUser.getSchoolId() != null){
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<SysAppUser> appUserList = appUserService.selectAppUserList(appUser);
        for (SysAppUser sysAppUser : appUserList) {
            if (sysAppUser.getSchoolId() == 0 && sysAppUser.getClassId() == 0) {
                sysAppUser.setIsBind("0");
            } else {
                sysAppUser.setIsBind("1");
            }
        }
        PageInfo<SysAppUser> pageInfo = new PageInfo<>(appUserList);
        return new ApiReturnObject("200", "查询成功", pageInfo);
//        }
//        return new ApiReturnObject("201","缺少schoolId","");
    }


    /**
     * 查询用户详情 PC 端
     */
    @GetMapping("/selectOne")
    public Map<String, String> selectOne(String id) {
        SysUser user1 = new SysUser();
        user1.setStrId(id);
        SysUser user = sysUserService.selectOneUser(user1);
        HashMap<String, String> map = Maps.newHashMap();
        if (user != null) {
            if (!"".equals(user.getSysRelname())) {
                map.put("sysRelName", user.getSysRelname());
            }
            if (!"".equals(user.getSysIco())) {
                map.put("sysIco", user.getSysIco());
            }
        }
        return map;
    }

    /**
     * 查询用户详情 PC 端
     */
    @GetMapping("/selectOneTwo")
    public Integer selectOneTwo(String id) {
        HashMap<String, Object> map = Maps.newHashMap();
        SysUser user = sysUserService.selectOne(id);
        map.put("schoolId", user.getSchoolId());
        userData user1 = new userData();
        user1.setSysType("2");
        user1.setSchoolId(user.getSchoolId());
        SysUser sysUser = sysUserService.selectByDeptId(user1);
        return sysUser.getId();
    }


    /**
     * 查询用户详情 PC 端
     */
    @GetMapping("/selectOneById")
    public userData selectOneById(Integer id) {
        SysUser user1 = new SysUser();
        user1.setId(id);
        SysUser user = sysUserService.selectOneUser(user1);
        if (StringUtils.isNotNull(user)) {
            return userData.builder().schoolId(user.getSchoolId()).userId(user.getId()).customerType(user.getCustomerType()).build();
        } else {
            return new userData();
        }
    }


    /**
     * 查询appCatalogController 类调用
     */
    @PostMapping("/selectByDeptId")
    public Integer selectByDeptId(@RequestBody userData data) {
        //设置条件
        SysUser user1 = sysUserService.selectByDeptId(data);
        return user1.getId();
    }


    /**
     * 查询用户详情 PC 端
     */
    @GetMapping("/selectOnePC")
    public ApiReturnObject selectOnePC(String id) {
        if (id.isEmpty() && "".equals(id)) {
            return new ApiReturnObject("201", "参数为空", "");
        }
        SysUser user1 = new SysUser();
        user1.setId(Integer.parseInt(id));
        SysUser user = sysUserService.selectOneUser(user1);
        String token = authClient.getToken(user.getId());
        user.setToken(token);
        user.setSysPassword("");
        Integer[] catalogId = wisdomFeign.selectCatalogId(Integer.parseInt(id));
        if (catalogId != null && catalogId.length > 0) {
            user.setCatalogIds(catalogId);
        } else {
            user.setCatalogIds(new Integer[2]);
        }
        SysUserVO vo = new SysUserVO();
        String[] split = null;
        if (StringUtils.isNotEmpty(user.getExpireDate())) {
            split = user.getExpireDate().split(",");
            vo.setAttendTimeString(split[0]);
            vo.setEndTimeString(split[1]);
        }
        BeanUtils.copyProperties(user, vo);
        return new ApiReturnObject("200", "查询成功", vo);
    }

    /**
     * 查询用户详情 PC 端
     */
    @GetMapping("/selectOnePCF")
    public SysUserOther selectOnePCF(String id) {
        SysUser user1 = new SysUser();
        user1.setId(Integer.parseInt(id));
        SysUser user = sysUserService.selectOneUser(user1);
        SysUserOther sysUserOther = new SysUserOther();
        if (StringUtils.isNotNull(user)) {
            BeanUtils.copyProperties(user, sysUserOther);
        }
        return sysUserOther;
    }

    /**
     * 查询用户详情 根据用户手机号
     */
    @GetMapping("/queryUserByUserPhone")
    SysUserOther queryUserByUserPhone(@RequestParam("userPhone") String userPhone) {
        SysUser user1 = new SysUser();
        user1.setSysPhone(userPhone);
        SysUser user = sysUserService.selectOneUser(user1);
        SysUserOther sysUserOther = new SysUserOther();
        if (StringUtils.isNotNull(user)) {
            BeanUtils.copyProperties(user, sysUserOther);
        }
        return sysUserOther;
    }


    /**
     * 查询用户详情 App 端
     */
    @GetMapping("/selectOneApp")
    public ApiReturnObject selectOneApp(String id) {
        SysAppUser sysAppUser1 = new SysAppUser();
        sysAppUser1.setId(Long.valueOf(id));
        SysAppUser sysAppUser = appUserService.selectOneAppUser(sysAppUser1);
        return new ApiReturnObject("200", "查询成功", sysAppUser);
    }

    /**
     * 新增保存系统用户
     * 管理员新增教务主任
     */
    @PostMapping("/addPCUser")
    public ApiReturnObject addPCUser(@RequestBody SysUserVO sysUser) {
        String start, end, expire;
        if (StringUtils.isNotEmpty(start = sysUser.getAttendTimeString()) && StringUtils.isNotEmpty(end = sysUser.getEndTimeString())) {
            expire = start + "," + end;
            sysUser.setExpireDate(expire);
        }
        if (sysUser.getSysPhone() != null && !"".equals(sysUser.getSysPhone())) {
            SysUser user1 = sysUserService.selectSysUserBysysPhone(sysUser.getSysPhone());
            if (user1 != null) {
                return new ApiReturnObject("201", "该手机号已存在", "");
            }
        }
        if (sysUser.getUsbKey() != null && !"".equals(sysUser.getUsbKey())) {
            SysUser user = new SysUser();
            user.setUsbKey(sysUser.getUsbKey());
            List<SysUser> sysUserList = sysUserService.select(user);
            if (sysUserList.size() != 0) {
                return new ApiReturnObject("202", "u盾密码已经存在", "");
            }
        }
        sysUser.setCreateTime(DateUtils.FdateTime());
        if (StringUtils.isBlank(sysUser.getSysPassword())) {
            sysUser.setSysPassword("123456");
        }
        sysUser.setSysPassword(Token.getMD5String(sysUser.getSysPassword()));
        //注册赠送用户20达人币
        sysUser.setPrice(new BigDecimal(20));
        sysUser.setSysType("3");
        sysUser.setCustomerType("2");
        sysUserService.insert(sysUser);

        Integer id = sysUser.getId();
        if (sysUser.getRoleId() != null) {
            //选择了角色  往中间表中添加数据
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(id);
            sysUserRole.setRoleId(sysUser.getRoleId());
            userRoleService.insert(sysUserRole);
        }
        return new ApiReturnObject("200", "添加成功", "");
    }

    /**
     * 新增保存系统用户  往中间表中添加数据  feign远程调用
     *
     * @param sysPhone
     * @param sysPassword
     * @param schoolId
     * @param tId
     * @param teacherName
     * @return
     */
    @GetMapping("/addUser")
    public ApiReturnObject addUser(@RequestParam("sysPhone") String sysPhone,
                                   @RequestParam("sysPassword") String sysPassword,
                                   @RequestParam("schoolId") Integer schoolId,
                                   @RequestParam("tId") Integer tId,
                                   @RequestParam("teacherName") String teacherName) {
        if (sysPhone != null && !"".equals(sysPhone)) {
            SysUser user1 = sysUserService.selectSysUserBysysPhone(sysPhone);
            if (user1 != null) {
                Map ops = new HashMap<String, Object>();
                ops.put("id", user1.getId());
                ops.put("type", user1.getSysType());
                return new ApiReturnObject("201", "该手机号已存在", ops);
            }
        }
        SysUser sysUser = new SysUser();
        sysUser.setCreateTime(DateUtils.FdateTime());
        sysUser.setSysPhone(sysPhone);
        sysUser.setSysPassword(Token.getMD5String(sysPassword));
        //注册赠送用户20达人币
        sysUser.setPrice(new BigDecimal(20));
        sysUser.setSchoolId(schoolId);
        sysUser.setSysRelname(teacherName);//姓名
        sysUser.setSysState(0);
        sysUser.setSysType("2");
        sysUser.setCustomerType("2");
        sysUserService.insert(sysUser);
        Integer id = sysUser.getId();
        //选择了角色  往中间表中添加数据
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserId(id);
        //老师-2
        sysUserRole.setRoleId(2);
        userRoleService.insert(sysUserRole);
        if (tId != null && 0 != tId) {
            //用户添加成功分配共享专业课程
            wisdomFeign.insUserBuy(sysUser.getId(), tId);
        }
        return new ApiReturnObject("200", "添加成功", id);
    }


    /**
     * 新增保存app端用户
     */
    @PostMapping("/addAppUser")
    public ApiReturnObject addAppUser(@RequestBody SysAppUser appUser) throws Exception {
        SysAppUser sysAppUser = new SysAppUser();
        if (appUser.getSysPhone() != null && !"".equals(appUser.getSysPhone())) {
            sysAppUser.setSysPhone(appUser.getSysPhone());
            List<SysAppUser> sysAppUsers = appUserService.selectAppUserList(sysAppUser);
            if (sysAppUsers.size() > 0) {
                return new ApiReturnObject("201", "该手机号已存在", "");
            }
        }
        if (!"".equals(appUser.getIdcardno())) {
            sysAppUser.setIdcardno(appUser.getIdcardno());
            List<SysAppUser> sysAppUsers = appUserService.selectAppUserList(sysAppUser);
            if (sysAppUsers.size() > 0) {
                return new ApiReturnObject("201", "该身份证号已存在", "");
            }
        }
        appUser.setCreateTime(new Date());
//        appUser.setSysPassword(Token.getMD5String(appUser.getSysPassword()));
        //设置密码
        appUser.setSysPassword(Token.encoderByMd5("123456"));
        appUserService.insert(appUser);
        //获取自动生成的id
        int id = appUser.getId().intValue();
        return new ApiReturnObject("200", "添加成功", id);
    }

    /**
     * 新增保存app端用户
     */
    @PostMapping("/addAppUser2")
    public ApiReturnObject addAppUser2(@RequestBody SysAppUser appUser) {
        appUser.setCreateTime(new Date());
//        appUser.setSysPassword(Token.getMD5String(appUser.getSysPassword()));
        //设置密码
        appUser.setSysPassword(Token.getMD5String("123456"));
        SysAppUser user = new SysAppUser();
        BeanUtils.copyProperties(appUser, user);
        appUserService.insert(user);
        //获取自动生成的id
        int id = user.getId().intValue();
        return new ApiReturnObject("200", "添加成功", id);
    }

    /**
     * 新增保存app端用户
     */
    @GetMapping("/addAppUser3")
    public ApiReturnObject addAppUser3(@RequestParam(value = "sysPhone") String sysPhone,
                                       @RequestParam(value = "classId") String classId,
                                       @RequestParam(value = "schoolId") Integer schoolId,
                                       @RequestParam(value = "idcardno", required = false) String idcardno,
                                       @RequestParam(value = "sysRelname") String sysRelname,
                                       @RequestParam(value = "sysType", required = false) Integer sysType) {
        SysUser user = new SysUser();
        user.setSysPhone(sysPhone);
        user.setClassId(Integer.valueOf(classId));
        user.setSchoolId(schoolId);
        user.setSysState(0);
        user.setIdcardno(idcardno);
        user.setSysRelname(sysRelname);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        user.setCreateTime(simpleDateFormat.format(new Date()));
        //设置密码
        user.setSysPassword(Token.getMD5String("123456"));
        //设置用户类型
        if (StringUtils.isNotNull(sysType)) {
            user.setSysType(sysType.toString());
            user.setCustomerType("2");
        }
        sysUserService.insert(user);
        //获取自动生成的id
        int id = user.getId();
        return new ApiReturnObject("200", "添加成功", id);
    }

    /**
     * 修改保存PC系统用户
     *
     * @param userId
     * @param classId
     * @return
     */
    @GetMapping("/updatePC2")
    public ApiReturnObject editSave2(@RequestParam("userId") Integer userId,
                                     @RequestParam("classId") Integer classId) {
        if (userId != null && classId != null) {
            SysUser sysUser = new SysUser();
            sysUser.setId(userId);
            sysUser.setClassId(classId);
            sysUserService.update(sysUser);
            return new ApiReturnObject("200", "修改成功", "");
        }
        return new ApiReturnObject("201", "缺少id", "");
    }

    /**
     * 修改保存PC系统用户
     */
    @GetMapping("/updateTea")
    public ApiReturnObject updateTea(@RequestParam("userId") Integer userId,
                                     @RequestParam("phone") String phone) {
        if (userId != null && phone != null) {
            SysUser sysUser1 = sysUserService.selectSysUserBysysPhone(phone);
            if (sysUser1 != null) {
                //已存在该手机号
                return new ApiReturnObject("201", "该手机号已存在", "");
            }
            SysUser sysUser = new SysUser();
            sysUser.setId(userId);
            sysUser.setSysPhone(phone);
            sysUserService.update(sysUser);
            return new ApiReturnObject("200", "修改成功", "");
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }

    /**
     * 修改保存PC系统用户
     */
    @PostMapping("/updatePC")
    public ApiReturnObject editSave(@RequestBody SysUserVO sysUser) {
        String start, end, expire;
        if (StringUtils.isNotEmpty(start = sysUser.getAttendTimeString()) && StringUtils.isNotEmpty(end = sysUser.getEndTimeString())) {
            expire = start + "," + end;
            sysUser.setExpireDate(expire);
        }
        if (sysUser.getId() != null) {
            if (sysUser.getRoleId() != null) {
                //先清空中间表的数据   再进行添加
                userRoleService.delete(sysUser.getId());
                //if(sysUser.getRoleList()!=null && sysUser.getRoleList().size() > 0){
                //选择了角色  往中间表中添加数据
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(sysUser.getId());
                //取到角色id 并添加
                //for (Integer integer : sysUser.getRoleList()) {
                sysUserRole.setRoleId(sysUser.getRoleId());
                //修改用户角色
                userRoleService.insert(sysUserRole);
            }
            //将密码进行加密
            if (sysUser.getSysPassword() != null && !"".equals(sysUser.getSysPassword())) {
                sysUser.setSysPassword(Token.getMD5String(sysUser.getSysPassword()));
            }
            sysUserService.update(sysUser);
            return new ApiReturnObject("200", "修改成功", "");
        }
        return new ApiReturnObject("201", "缺少id", "");
    }

    /**
     * 修改保存App系统用户
     */
    @PostMapping("/updateApp")
    public ApiReturnObject editSaveApp(@RequestBody SysUser appUser) {
        if (appUser.getId() != null) {
            //将密码进行加密
            if (appUser.getSysPassword() != null && !"".equals(appUser.getSysPassword())) {
                appUser.setSysPassword(Token.getMD5String(appUser.getSysPassword()));
            }
            sysUserService.update(appUser);
            return new ApiReturnObject("200", "修改成功", "");
        }
        return new ApiReturnObject("201", "缺少id", "");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @GetMapping("/delete")
    public ApiReturnObject deleteById(String ids) {
        String[] split = ids.split(",");
        for (String s : split) {
            sysUserService.deleteById(Integer.parseInt(s));
            userRoleService.delete(Integer.parseInt(s));
        }
        return new ApiReturnObject("200", "success", "");
    }

    /**
     * 删除PC用户
     */
    @PostMapping("/delPC")
    public ApiReturnObject delPC(@RequestBody SysUser user) {
        if (user.getId() != null) {
            //先删除中间表数据  再删除用户相关数据
            userRoleService.delete(user.getId());
            sysUserService.deleteById(user.getId());
            return new ApiReturnObject("200", "删除成功", "");
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }

    /**
     * 删除PC用户
     */
    @GetMapping("/delPCUser")
    public ApiReturnObject delPCUser(Integer userId) {
        if (userId != null) {
            //先删除中间表数据  再删除用户相关数据
            userRoleService.delete(userId);
            sysUserService.deleteById(userId);
            return new ApiReturnObject("200", "删除成功", "");
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }

    /**
     * 删除App用户
     */
    @PostMapping("/delApp")
    public ApiReturnObject delApp(@RequestBody SysAppUser appUser) {
        if (appUser.getId() != null) {
            //先删除中间表数据  再删除用户相关数据
//            userRoleService.delete(user.getId());
            appUserService.deleteById(appUser.getId().intValue());
            return new ApiReturnObject("200", "删除成功", "");
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }


    /**
     * 修改用户信息
     */
    @PostMapping("/updateImg")
    public ApiReturnObject editSaveImg(@RequestBody SysUser sysUser) {
        if (baiduAipContentCensor.AipImage(sysUser.getSysIco()) == 2) {
            return new ApiReturnObject("201", "头像内容违规！请重新上传！", "");
        }
        //修改头像
        //先清空中间表的数据   再进行添加
        if (sysUser.getId() != null && sysUser.getSysIco() != null && !"".equals(sysUser.getSysIco())) {
            sysUserService.updateImg(sysUser);
            return new ApiReturnObject("200", "修改成功", sysUser.getSysIco());
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }


    @GetMapping("/updatePasswordByPhone")
    public ApiReturnObject updatePasswordByPhone(@RequestParam("sysPhone") String sysPhone,
                                                 @RequestParam("sysPassword") String sysPassword) {
        SysUser sysUser = new SysUser();
        sysUser.setSysPhone(sysPhone);
        sysUser.setSysPassword(sysPassword);
        sysUserService.updatePassword(sysUser);
        return new ApiReturnObject("200", "修改成功", "");
    }


    /**
     * feign : 根据用户id查询用户信息
     */
    @GetMapping("/findByUserId")
    public Map<String, Object> findByUserId(Integer userId) {
        SysUser sysUser = sysUserService.selectByPrimaryKey(userId);
        if (sysUser != null) {
            Map<String, Object> map = new HashMap<>(6);
            //头像
            map.put("image", sysUser.getSysIco());
            //姓名
            map.put("name", sysUser.getSysRelname());
            //价格
            map.put("price", sysUser.getPrice());
            //classId
            map.put("classId", sysUser.getClassId());
            //用户id
            map.put("id", sysUser.getId());
            //学校id
            map.put("schoolId", sysUser.getSchoolId());
            return map;
        } else {
            return null;
        }
    }

    /**
     * feign : 根据用户id查询用户信息
     */
    @GetMapping("/findUserBySchoolId")
    public boolean selectUserListFeign(@RequestParam("schoolId") Integer schoolId,
                                       @RequestParam("phone") String phone) {
        SysUser sysUser = new SysUser();
        sysUser.setSchoolId(schoolId);
        sysUser.setSysPhone(phone);
        SysUser sysUser1 = sysUserService.selectUserListFeign(sysUser);
        return sysUser1 != null;
    }

    /**
     * feign : 根据用户id查询用户信息
     */
    @GetMapping("/findUserBySchoolId2")
    public boolean selectUserListFeign2(@RequestParam("phone") String phone) {
        SysUser sysUser = new SysUser();
        sysUser.setSysPhone(phone);
        SysUser sysUser1 = sysUserService.selectUserListFeign(sysUser);
        return sysUser1 != null;
    }

    /**
     * feign : 根据用户手机号查询用户id
     */
    @GetMapping("/findSysUserBysysPhone")
    public ApiReturnObject selectSysUserBysysPhone(@RequestParam("phone") String phone) {
        SysUser sysUser = new SysUser();
        sysUser.setSysPhone(phone);
        SysUser sysUser1 = sysUserService.selectUserListFeign(sysUser);
        return new ApiReturnObject("200", "查询成功", sysUser1.getId());
    }

    /**
     * feign : 根据用户id修改用户达人币数量
     */
    @PutMapping("/updateByPCUserId")
    public String updateByPCUserId(@RequestParam(value = "userId", required = true) Integer userId,
                                   @RequestParam(value = "price", required = true) BigDecimal price) {
        SysUser user = new SysUser();
        user.setId(userId);
        user.setPrice(price);
        Integer i = sysUserService.updateById(user);
        if (i > 0) {
            return "success";
        } else {
            return null;
        }
    }

    /**
     * feign : 添加学生用户   手机号 身份证号 姓名 班级id
     */
    @GetMapping("/addStuUser")
    public ApiReturnObject addStuUser(@RequestParam("sysPhone") String sysPhone,
                                      @RequestParam("classId") Integer classId,
                                      @RequestParam("idcardno") String idcardno,
                                      @RequestParam("studentName") String studentName) {
        if (sysPhone != null && !"".equals(sysPhone)) {
            SysUser user1 = sysUserService.selectSysUserBysysPhone(sysPhone);
            if (user1 != null) {
                return new ApiReturnObject("201", "该手机号已存在", "");
            }
        }
        SysUser sysUser = new SysUser();
        sysUser.setSysPhone(sysPhone);
        sysUser.setIdcardno(idcardno);
        sysUser.setClassId(classId);
        //姓名
        sysUser.setSysRelname(studentName);

        sysUser.setCreateTime(DateUtils.FdateTime());
        sysUser.setSysPassword(Token.getMD5String("123456"));
        //注册赠送用户20达人币
        sysUser.setPrice(new BigDecimal(20));
        sysUser.setSysIco("4.0defaultavatar.png");
        sysUser.setSysState(0);
        sysUserService.insert(sysUser);
        return new ApiReturnObject("200", "添加成功", sysUser.getId());
    }

    @GetMapping("/selectUserById")
    public User selectUserById(@RequestParam(value = "userId") Integer userId) {
        return sysUserService.selectUserById(userId);
    }

    @PostMapping("/updatePassword")
    Integer updatePassword(@RequestBody User user) {
        return sysUserService.updateNewPassword(user);
    }

}